package com.john.blog.common.vo;

import com.john.blog.common.utils.DetectUtils;

/**
 * ftp配置对象
 * 
 * @author JiangGengchao
 * @date 2018年3月11日
 */
public class FtpConfig {

    String ip;
    int port;
    String username;
    String password;
    String defaultRemoteDir;
    String encoding = "gbk";

    public FtpConfig() {

    }

    /**
     * ip=192.168.120.152;port=21;username=irmds;password=aaaaaa;defaultDir=/
     * attachment;encoding=utf-8 <br />
     * 以分号分隔的key=value的字符串, 顺序可以不固定, 但key值要保持一致
     * ip,port,username,password,defaultDir,encoding <br />
     * 
     * @param formatString
     */
    public FtpConfig(String formatString) {
        if (DetectUtils.notEmpty(formatString)) {
            String[] configs = formatString.split(";");
            if (DetectUtils.notEmpty(configs)) {
                for (int i = 0; i < configs.length; i++) {
                    String key = configs[i].split("=")[0];
                    String value = configs[i].split("=")[1];
                    if ("ip".equalsIgnoreCase(key)) {
                        this.ip = value;
                    } else if ("port".equalsIgnoreCase(key)) {
                        this.port = Integer.valueOf(value);
                    } else if ("username".equalsIgnoreCase(key)) {
                        this.username = value;
                    } else if ("password".equalsIgnoreCase(key)) {
                        this.password = value;
                    } else if ("defaultDir".equalsIgnoreCase(key)) {
                        this.defaultRemoteDir = value;
                    } else if ("encoding".equalsIgnoreCase(key)) {
                        this.encoding = value;
                    }
                }
            }
        }
    }

    public FtpConfig(String ip, int port, String username, String password, String defaultRemoteDir, String encoding) {
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
        this.defaultRemoteDir = defaultRemoteDir;
        if (null != encoding) {
            this.encoding = encoding;
        }
    }

    public boolean initFinished() {
        return DetectUtils.notEmpty(ip) && (0 < port) && DetectUtils.notEmpty(username) && DetectUtils.notEmpty(password) && DetectUtils.notEmpty(encoding);
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDefaultRemoteDir() {
        return defaultRemoteDir;
    }

    public void setDefaultRemoteDir(String defaultRemoteDir) {
        this.defaultRemoteDir = defaultRemoteDir;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

}